#include "main_interface.h"
#include "ui_main_interface.h"

#include<qDebug>
#include<QTreeWidgetItem>

MainInterface::MainInterface(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainInterface)
{
    ui->setupUi(this);

    //initializeTreeView();
    initializeTreeWidget();

}

MainInterface::~MainInterface()
{
    delete ui;
}

void MainInterface::GetNode()
{
    for(int i = 0;i < mModel->rowCount() ;i++)
    {
        QStandardItem *item = mModel->item(i);

        qDebug() <<"item = " << item->text();
        qDebug()<<"GetNode() i:"<<i;

        GetItem(mModel->item(i) );
    }
}

void MainInterface::GetItem(QStandardItem *item)
{
    Q_ASSERT(item);
    if(item->hasChildren())
    {
        for(int i = 0;i < item->rowCount() ;i++)
        {
            QStandardItem * childitem = item->child(i);

            qDebug() << "childitem = " << childitem->text();
            qDebug()<<"GetItem() i:"<<i;

            GetItem(childitem);
        }
    }
}

void MainInterface::initializeTreeView()
{
    mModel = new QStandardItemModel(ui->treeView);


    ui->treeView->setEditTriggers(QTreeView::NoEditTriggers);			//单元格不能编辑
    ui->treeView->setSelectionBehavior(QTreeView::SelectRows);			//一次选中整行
    ui->treeView->setSelectionMode(QTreeView::SingleSelection);        //单选，配合上面的整行就是一次选单行
    //ui->treeView->setAlternatingRowColors(true);                       //每间隔一行颜色不一样，当有qss时该属性无效
    ui->treeView->setFocusPolicy(Qt::NoFocus);                         //去掉鼠标移到单元格上时的虚线框


    //设置数据
    //一级节点
    QStandardItem* itemGrade = new QStandardItem(QString("成绩管理"));
    mModel->appendRow(itemGrade);       //一级节点挂在model上

    //二级节点：
    QStandardItem* tmpItem1 = new QStandardItem(QString("运行前检查"));
    QStandardItem* tmpItem2 = new QStandardItem(QString("检查与实验"));
    QStandardItem* tmpItem3 = new QStandardItem(QString("线路驾驶"));
    itemGrade->appendRow(tmpItem1);    //二级节点挂在一级的第1列节点上
    itemGrade->appendRow(tmpItem2);    //二级节点挂在一级的第1列节点上
    itemGrade->appendRow(tmpItem3);    //二级节点挂在一级的第1列节点上

    QStandardItem* itemGrade2 = new QStandardItem(QString("信息管理"));
    mModel->appendRow(itemGrade2);       //一级节点挂在model上

    //二级节点：
    QStandardItem* tmpItem4 = new QStandardItem(QString("学生信息"));
    QStandardItem* tmpItem5 = new QStandardItem(QString("教师信息"));
    QStandardItem* tmpItem6 = new QStandardItem(QString("角色信息"));
    itemGrade2->appendRow(tmpItem4);    //二级节点挂在一级的第1列节点上
    itemGrade2->appendRow(tmpItem5);    //二级节点挂在一级的第1列节点上
    itemGrade2->appendRow(tmpItem6);    //二级节点挂在一级的第1列节点上

    // mModel->appendRow(itemGrade2);       //一级节点挂在model上

    //应用model
    ui->treeView->setModel(mModel);

    //&QTreeView::doubleClicked,
    connect(ui->treeView, &QTreeView::doubleClicked,
            this,[&](const QModelIndex& modelIndex){

                GetNode();

                // //
                // // QModelIndex tmpIndex =  mModel->index(childIndex.row(), childIndex.column());
                // // QVariant keyValue = mModel->data(tmpIndex);
                // qDebug()<<data.toString() ;

                ////
                qDebug()<<modelIndex.row()<<" "<<modelIndex.column();
                //  QStandardItem *item = mModel->item(modelIndex.row());
                // if(item->hasChildren()){
                //      QStandardItem * childitem = item->child(modelIndex.column());
                //      qDebug() << "childitem = " << childitem->text();
                //  }

            });

}

void MainInterface::initializeTreeWidget()
{
    ui->treeWidget->clear();

    // 设置QTreeWidget的列数
    ui->treeWidget->setColumnCount(1);
    // 设置QTreeWidget标题隐藏
    ui->treeWidget->setHeaderHidden(true);

    /* 成绩管理 */
    QTreeWidgetItem  *gradeItem = new QTreeWidgetItem(true);
    gradeItem->setText(0,"成绩管理");
    gradeItem->setIcon(0,QIcon(":/Image/favicon.ico"));  // 添加一个图标
    ui->treeWidget->addTopLevelItem(gradeItem);

    // 给成绩节点添加一个三个节点
    QTreeWidgetItem* examineOneGradeItem = new QTreeWidgetItem(gradeItem);
    examineOneGradeItem->setText(0,"运行前检查");
    examineOneGradeItem->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    //frd->setCheckState(0,Qt::Checked);               // 默认选中状态
    gradeItem->addChild(examineOneGradeItem);

    QTreeWidgetItem* examineTwoGradeItem = new QTreeWidgetItem(gradeItem);
    examineTwoGradeItem->setText(0,"检查与实验");
    examineTwoGradeItem->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    gradeItem->addChild(examineTwoGradeItem);


    QTreeWidgetItem* routeGradeItem = new QTreeWidgetItem(gradeItem);
    routeGradeItem->setText(0,"线路驾驶");
    routeGradeItem->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    gradeItem->addChild(routeGradeItem);

    /* 信息管理 */
    QTreeWidgetItem  *informationManageItem = new QTreeWidgetItem(true);
    informationManageItem->setText(0,"信息管理");
    informationManageItem->setIcon(0,QIcon(":/Image/favicon.ico"));  // 添加一个图标
    ui->treeWidget->addTopLevelItem(informationManageItem);

    QTreeWidgetItem* studentManageItem = new QTreeWidgetItem(informationManageItem);
    studentManageItem->setText(0, "学员信息管理");
    studentManageItem->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    informationManageItem->addChild(studentManageItem);

    QTreeWidgetItem* teacherManageItem = new QTreeWidgetItem(informationManageItem);
    teacherManageItem->setText(0, "教师信息管理");
    teacherManageItem->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    informationManageItem->addChild(teacherManageItem);

    QTreeWidgetItem* roleManageItem = new QTreeWidgetItem(informationManageItem);
    roleManageItem->setText(0, "角色信息管理");
    roleManageItem->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    informationManageItem->addChild(roleManageItem);

     connect(ui->treeWidget, &QTreeWidget::itemClicked, this, [&](QTreeWidgetItem *item, int column)
    {
        QString str = item->text(column);
        qDebug() << str;
        ui->plainTextEdit->appendPlainText(str.toStdString().data());
    });


    // // 创建QTreeWidget的朋友节点,父节点是tree
    // QTreeWidgetItem *Friend = new QTreeWidgetItem(ui->treeWidget,QStringList(QString("朋友")));
    // Friend->setIcon(0,QIcon(":/image/4.ico"));  // 添加一个图标
    // Friend->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable
    //                  | Qt::ItemIsEnabled | Qt::ItemIsAutoTristate);
    // Friend->setCheckState(0,Qt::Checked);

    // // 给Friend添加一个子节点frd
    // QTreeWidgetItem *frd = new QTreeWidgetItem(Friend);
    // frd->setText(0,"www.lyshark.com");
    // frd->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    // frd->setCheckState(0,Qt::Checked);               // 默认选中状态

    // QTreeWidgetItem *frs = new QTreeWidgetItem(Friend);
    // frs->setText(0,"cdn.lyshark.com");
    // frs->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    // frs->setCheckState(0,Qt::Unchecked);            // 默认未选中

    // // ----------------------------------------------------------
    // // 创建名叫同学节点,父节点同样是tree
    // QTreeWidgetItem * ClassMate = new QTreeWidgetItem(ui->treeWidget,QStringList(QString("同学")));
    // ClassMate->setIcon(0,QIcon(":/Image/favicon.ico"));  // 添加一个图标
    // ClassMate->setCheckState(0,Qt::Checked);       // 默认选中

    // //Fly是ClassMate的子节点
    // QTreeWidgetItem *Fly = new QTreeWidgetItem(QStringList(QString("nas.lyshark.com")));
    // Fly->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    // //创建子节点的另一种方法
    // ClassMate->addChild(Fly);
    // Fly->setCheckState(0,Qt::Checked);       // 设置为选中

    // QTreeWidgetItem *Fls = new QTreeWidgetItem(QStringList(QString("lyshark.cnblogs.com")));
    // Fls->setIcon(0,QIcon(tr(":/Image/favicon.ico")));
    // ClassMate->addChild(Fls);
    // Fls->setCheckState(0,Qt::Checked);       // 设置为选中

    // // ----------------------------------------------------------
    // // 陌生人单独一栏
    // QTreeWidgetItem  *Strange = new QTreeWidgetItem(true);
    // Strange->setText(0,"陌生人");
    // Strange->setIcon(0,QIcon(":/Image/favicon.ico"));  // 添加一个图标

    // ui->treeWidget->addTopLevelItem(ClassMate);
    // ui->treeWidget->addTopLevelItem(Strange);

    // // 增加文本到编辑框
    // ui->plainTextEdit->appendPlainText("测试开始");

    // //展开QTreeWidget的所有节点
    // //ui->treeWidget->expandAll();
    // //ui->treeWidget->resize(271,401);

    // // connect(ui->treeWidget, &QTreeWidget::doubleClicked, this, [&](QTreeWidgetItem *item, int column)
    // // {
    // //     QString str = item->text(column);
    // //     qDebug() << str;
    // //     ui->plainTextEdit->appendPlainText(str.toStdString().data());
    // // });


}




